require 'smarter_csv'
require 'csv'

class ProductsImporter

  attr_accessor :not_imported

  def self.import(file)
    begin
      @not_imported = Array.new
      options = { :chunk_size => 10 }
      #spreadsheet = ProductsImporter.open_spreadsheet(file)
      SmarterCSV.process(file.path) do |chunks|
        chunks.each do |spreadsheet|
          ProductsImporter.parse(spreadsheet)
        end
      end
      @not_imported
    rescue Exception => e
      raise e
    end
  end

  #def self.parse(spreadsheet)
  def self.parse(row)
    begin
      products = []
      @product = ProductsImporter.row_to_product(row)
	    products << @product unless @product.blank?
      #return ProductsImporter.delete_duplicates(recipients)

      #products = []
      #spreadsheet.each do |i|
	      #row = spreadsheet.row(i)
	      #@product = ProductsImporter.row_to_product(row)
	      #products << @product unless @product.blank?
      #end
      #return ProductsImporter.delete_duplicates(recipients)
    rescue NoMethodError => e
      raise BadFormatFileException.new(e.message)
    rescue Exception => e
      raise e
    end
  end
  
  def self.delete_duplicates(recipients)
    begin
      hash = {}
      
      recipients.each do |r|
	first_name = r.first_name || ""
	last_name = r.last_name || ""
	email = r.email
	key = "#{first_name.purge} #{last_name.purge} #{email.purge}"
	hash.merge!({key.to_sym => r})
      end
      
      recipients = []
      hash.each do |k,v|
	recipients << v
      end
    
      return recipients
    rescue NoMethodError => e
      raise BadFormatFileException.new(e.message)
    rescue Exception => e
      raise e
    end
  end
  
  def self.open_spreadsheet(file)
    Csv.new(file.path, {})
  end
  
  def self.row_to_product(row)
    #unless row.compact.blank?
      begin
        ProductsImporter.format(row)
      rescue
        @not_imported << row[:modelo]
      end
    #else
    #  nil
    #end
  end
  
  def self.format(row)
    # Format:
    # 0: Marca, 1: Modelo, 2: Precio, 3: Precio promocional, 4:	Colores, 5: Mostrar en home, 6:	Agotado, 7:	Visible,
    # 8: Acero, 9: Bijou, 10:	Caucho, 11:	Cronógrafos, 12:	Cuero, 13:	Metal, 14:	Despertador, 15:	Varios,
    #	16: Dama, 17:	Caballero

    begin
      brand = row[:marca]
      model = row[:modelo]
      picture_name = row[:nombre_foto]
      price = row[:precio]
      branch_price = row[:precio_franquicia]
      promotional_price = row[:precio_promocional]
      branch_promotional_price = row[:precio_promocional_franquicia]
      colors = row[:colores] || ""
      show_at_home = row[:mostrar_en_home]
      sold_out = row[:agotado]
      visible = row[:visible]
      acero = row[:acero] || "0"
      bijou = row[:bijou] || "0"
      caucho = row[:caucho] || "0"
      cronografos = row[:cronógrafos] || "0"
      cuero = row[:cuero] || "0"
      metal = row[:metal] || "0"
      despertador = row[:despertador] || "0"
      varios = row[:varios] || "0"
      dama = row[:dama] || "0"
      caballero = row[:hombre] || "0"
=begin
      brand = row[0]
      model = row[1]
      picture_name = row[2]
      price = row[3]
      promotional_price = row[4]
      colors = row[5] || ""
      show_at_home = row[6]
      sold_out = row[7]
      visible = row[8]
      acero = row[9] || "0"
      bijou = row[10] || "0"
      caucho = row[11] || "0"
      cronografos = row[12] || "0"
      cuero = row[13] || "0"
      metal = row[14] || "0"
      despertador = row[15] || "0"
      varios = row[16] || "0"
      dama = row[17] || "0"
      caballero = row[18] || "0"
=end
      product = Product.new_from_csv(
                          brand,
                          model,
                          picture_name,
                          price,
                          branch_price,
                          promotional_price,
                          branch_promotional_price,
                          colors,
                          show_at_home,
                          sold_out,
                          visible,
                          acero,
                          bijou,
                          caucho,
                          cronografos,
                          cuero,
                          metal,
                          despertador,
                          varios,
                          dama,
                          caballero
                )
    rescue Exception => e
      raise e
    end
  end
end